﻿using EquipmentAlarmSystem.Service.Models;
using System.Management;
using System.Net.Http.Json;
using System.Text.Json;

namespace 设备报警系统客户端
{
    internal static class Basic
    {
        public static string ServerAddr = "http://127.0.0.1:9999";
        #region 获取系统Id
        /// <summary>
        /// 获取系统Id
        /// </summary>
        /// <returns></returns>
        public static string GetSystemId()
        {
            string systemId = null;
            using (ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_ComputerSystemProduct"))
            {
                foreach (var item in mos.Get())
                {
                    systemId = item["UUID"].ToString()!;
                }
            }
            return systemId.Replace("-", "");
        }
        #endregion

        /// <summary>
        /// 时间统一格式化
        /// </summary>
        public static string GetDate
        {
            get
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            }
        }

        /// <summary>
        /// HttpPost
        /// </summary>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static async Task<string> HttpPostAsync(string url, object content)
        {
            using (var client = new HttpClient())
            {
                // 发送POST请求  
                HttpResponseMessage response = await client.PostAsJsonAsync($"{ServerAddr}{url}", content);

                // 确保HTTP成功状态值  
                response.EnsureSuccessStatusCode();

                // 读取响应内容  
                string responseBody = await response.Content.ReadAsStringAsync();

                await Console.Out.WriteLineAsync(responseBody);

                return responseBody;
            }
        }

        /// <summary>
        /// HttpGet
        /// </summary>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            try
            {
                var client = new HttpClient();
                var response = client.GetAsync(new Uri($"{ServerAddr}{url}")).Result;
                return response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 判断是否未空
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string iSNull(object o)
        {
            if (o is null)
            {
                return "-1";
            }
            return o.ToString()!;
        }

        /// <summary>
        /// 配置获取
        /// </summary>
        public static Configure Config;

        /// <summary>
        /// 获取配置文件
        /// </summary>
        public static void GetConfig()
        {
            Config = JsonSerializer.Deserialize<Configure>(HttpGet("/Configure/GetConfigure"));
        }
    }
}
